<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Vue中的Js动画与Velocity.js的结合</title>
    
    <style>
        .resc{
            width: 100px;
            height: 100px;
            background: #4dd0e1;
            color: #fff;
            /* line-height: 100px; */
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="root">
        <!-- :css-"false" vue跳过CSS检测 也可以避免过渡过程中css的影响 -->
        <transition
        name="fade"
        @before-enter="handleBeforeEnter"
        @enter="handleEnter"
        @after-enter="handleAfterEnter"
        @before-leave="handleBeforeLeave"
        @leave="handleLeave"
        @after-leave="handleAfterLeave"
        :css="false"
        >
            <div v-if="show" class="resc">hello vue and velocity.js!</div>
        </transition>
        <button @click="handleBtnClick">切换</button>
    </div>
    <script src="./js/vue.js"></script>
    <script src="./js/velocity.mim.js"></script>
    <script>
        var vm = new Vue({
            el: "#root",
            data: {
                show: false,
            },
            // 使用volocity.js实现动画效果
            methods: {
                handleBtnClick: function () {
                    this.show = !this.show;
                },
                handleBeforeEnter: function (el) {
                    // el指被transition包裹的div
                    el.style.opacity = 0;
                    console.log("动画即将执行...");

                },
                handleEnter: function (el, done) {
                    // done是一个回调函数
                    Velocity(el, 'stop');
                    Velocity(el, {
                        backgroundColor: '#0085eb',
                        opacity: 1,
                        translateX: 260,
                        rotateZ: ['360deg', 0]
                    }, {
                        duration: 1000,
                        easing: [ 0.4, 0.01, 0.165, 0.99 ],
                        complete: done // 这是必须写的，表示动画执行完毕，同时调用handleAfterEnter函数(动画结尾时的回调函数)
                    });
                    console.log('方块显示动画执行中')

                },
                handleAfterEnter: function (el) {
                    console.log('方块显示动画结束');
                },
                handleBeforeLeave: function(el){
                    console.log('方块隐藏动画即将执行...');
                },
                handleLeave: function(el, done){
                    Velocity(el, 'stop');
                    Velocity(el, {
                        backgroundColor: '#4dd0e1',
                        opacity: 0,
                        translateX: 0,
                        rotateZ: [0, '360deg']
                    }, {
                        duration: 1000,
                        easing: [ 0.4, 0.01, 0.165, 0.99 ],
                        complete: done,
                    });
                    // console.log('方块隐藏动画进行中...');
                },
                handleAfterLeave: function(el){
                    console.log('方块隐藏动画结束');
                }
            },

            // 原生动画
            // methods: {
            //     handleBtnClick: function(){
            //         this.show = !this.show;
            //     },
            //     handleBeforeEnter: function(el){
            //         el指被transition包裹的div
            //         el.style.color = "red";
            //     },
            //     handleEnter: function(el, done){
            //         setTimeout(() => {
            //             el.style.color = "green";
            //         }, 2000);
                    
            //         done是一个回调函数
            //         setTimeout(() => {
            //             done();
            //         },4000);
                    
            //     },
            //     handleAfterEnter: function(el){
            //         el.style.color = "black"
            //     }
            // }
        });
    </script>

</body>
</html>